"""
Case Type   : HTAP行列融合特性
Case Name   : 极限测试场景:300+分区表
Create At   : 2025/1/17
Owner       : ningyali
Description :
    1.建分区表1000分区
    2.插入1000000行数据
    3.进行全表转换
    4.查看query_imcstore_views()
    5.查询
    6.取消转换
    7.进行部分列转换
    8.查看query_imcstore_views()
    9.查询
    10.取消转换
    11.进行指定分区转换
    12.查看query_imcstore_views()
    13.查询
    14.取消转换
    15.进行指定分区部分列转换
    16.查看query_imcstore_views()
    17.查询
    18.清理数据环境
    19.查看query_imcstore_views()
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功
    6.执行成功
    7.执行成功
    8.执行成功
    9.执行成功
    10.执行成功
    11.执行成功
    12.执行成功
    13.执行成功
    14.执行成功
    15.执行成功
    16.执行成功
    17.执行成功
    18.执行成功
    19.执行成功
History     :
"""

import os
import re
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.t_name = 't_htap_single_0156'
        self.max_num = 1000
        self.step = 100
        self.part_list = [list(range(2, self.max_num))[
                          i * self.step:i * self.step + self.step] for i in
                          range(self.max_num // self.step)]

    def test_trans(self):
        text = '----step1:建分区表300+分区;expect:执行成功----'
        self.log.info(text)
        sql = f'''drop table if exists {self.t_name};
            create table {self.t_name}(
                c1 integer(20),
                c2 varchar(20) not null default '',
                c3 char(30)
            )
            partition by range (c1)
            (partition {self.t_name}_p1 values less than (1001));'''
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('CREATE TABLE', sql_res, f'执行失败:{text}')

        self.log.info('增加分区，避免建表语句过长报错Argument list too long')
        for i in self.part_list:
            part_sql = ""
            for j in i:
                part_sql += f"alter table {self.t_name} add partition " \
                            f"{self.t_name}_p{j} values less than " \
                            f"({j * 1000 + 1});\n"
            sql_res = self.pri_sh.execut_db_sql(
                part_sql, env_path=macro.SINGLE_DB_ENV_PATH)
            self.assertLessEqual(sql_res.count('ALTER TABLE'), self.step,
                                 f'增加分区失败:{text}')

        text = '----step2:插入数据; expect:执行成功----'
        self.log.info(text)
        sql = f'insert into {self.t_name} values (' \
              f'generate_series(1, {(self.max_num - 1) * 1000}),' \
              f'generate_series(1, {(self.max_num - 1) * 1000})||\'aaa\',' \
              f'generate_series(1, {(self.max_num - 1) * 1000})||\'bbb\');'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn(f'INSERT 0 {(self.max_num - 1) * 1000}', sql_res,
                      f'执行失败:{text}')

        text = '----step3:全表转换; expect:执行成功----'
        self.log.info(text)
        sql = f"alter table {self.t_name} imcstored;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('ALTER TABLE', sql_res, f'执行失败:{text}')

        text = '----step4:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect = [
            re.search(f'{self.t_name}_p{j}\\s+|\\s+1 2 3\\s+|'
                      f'\\s+3\\s+|\\s+COMPLETE',
                      sql_res) for i in self.part_list for j in i]
        self.assertNotIn(None, expect, f'执行失败:{text}')

        text = '----step5:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(*) from {self.t_name};' \
              f'select count(*) from {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn(f'{(self.max_num - 1) * 1000}', sql_res, f'执行失败:{text}')

        text = '----step6:取消转换; expect:执行成功----'
        self.log.info(text)
        sql = f"alter table {self.t_name} unimcstored;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('ALTER TABLE', sql_res, f'执行失败:{text}')

        text = '----step7:部分列转换; expect:执行成功----'
        self.log.info(text)
        sql = f"alter table {self.t_name} imcstored(c1,c2);"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('ALTER TABLE', sql_res, f'执行失败:{text}')

        text = '----step8:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect = [
            re.search(f'{self.t_name}_p{j}\\s+|\\s+1 2\\s+|'
                      f'\\s+2\\s+|\\s+COMPLETE',
                      sql_res) for i in self.part_list for j in i]
        self.assertNotIn(None, expect, f'执行失败:{text}')

        text = '----step9:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(c2) from {self.t_name};' \
              f'select count(c2) from {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn(f'{(self.max_num - 1) * 1000}', sql_res, f'执行失败:{text}')

        text = '----step10:取消转换; expect:执行成功----'
        self.log.info(text)
        sql = f"alter table {self.t_name} unimcstored;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('ALTER TABLE', sql_res, f'执行失败:{text}')

        text = '----step11:指定分区转换; expect:执行成功----'
        self.log.info(text)
        for i in self.part_list:
            part_sql = ""
            for j in i:
                part_sql += f"alter table {self.t_name} modify " \
                            f"partition {self.t_name}_p{j} imcstored;\n"
            sql_res = self.pri_sh.execut_db_sql(
                part_sql, env_path=macro.SINGLE_DB_ENV_PATH)
            self.assertLessEqual(sql_res.count('ALTER TABLE'), self.step,
                                 f'执行失败:{text}')

        text = '----step12:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect = [
            re.search(f'{self.t_name}_p{j}\\s+|\\s+1 2 3\\s+|'
                      f'\\s+3\\s+|\\s+COMPLETE',
                      sql_res) for i in self.part_list for j in i]
        self.assertNotIn(None, expect, f'执行失败:{text}')

        text = '----step13:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(*) from {self.t_name} ' \
              f'partition({self.t_name}_p3);' \
              f'select count(c2) from {self.t_name} ' \
              f'partition({self.t_name}_p3);'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn('1000', sql_res, f'执行失败:{text}')

        text = '----step14:取消转换; expect:执行成功----'
        self.log.info(text)
        sql = f"alter table {self.t_name} unimcstored;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('ALTER TABLE', sql_res, f'执行失败:{text}')

        text = '----step15:指定分区部分列转换; expect:执行成功----'
        self.log.info(text)
        for i in self.part_list:
            part_sql = ""
            for j in i:
                part_sql += f"alter table {self.t_name} modify " \
                            f"partition {self.t_name}_p{j} imcstored(c1,c3);\n"
            sql_res = self.pri_sh.execut_db_sql(
                part_sql, env_path=macro.SINGLE_DB_ENV_PATH)
            self.assertLessEqual(sql_res.count('ALTER TABLE'), self.step,
                                 f'执行失败:{text}')

        text = '----step16:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect = [
            re.search(f'{self.t_name}_p{j}\\s+|\\s+1 3\\s+|'
                      f'\\s+2\\s+|\\s+COMPLETE',
                      sql_res) for i in self.part_list for j in i]
        self.assertNotIn(None, expect, f'执行失败:{text}')

        text = '----step17:查询; expect:执行成功----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(c3) from {self.t_name} ' \
              f'partition({self.t_name}_p3);' \
              f'select count(c3) from {self.t_name} ' \
              f'partition({self.t_name}_p3);'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertIn('1000', sql_res, f'执行失败:{text}')

    def tearDown(self):
        text1 = '----step18:清理环境;expect:执行成功----'
        self.log.info(text1)
        sql = f'drop table if exists {self.t_name};'
        sql_res1 = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)

        text2 = '----step19:查看query_imcstore_views; expect:执行成功----'
        self.log.info(text2)
        sql = f"select relname,imcs_attrs,imcs_nattrs," \
              f"imcs_status,is_partition " \
              f"from query_imcstore_views() " \
              f"where relname like '{self.t_name}%' order by relname;"
        sql_res2 = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)

        self.assertIn('DROP TABLE', sql_res1, f'删除表失败:{text1}')
        self.assertIn('0 rows', sql_res2,
                      f'删除表后未清理query_imcstore_views:{text2}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
